package cn.itzd.controller;

import cn.itzd.entity.vo.SysFileVo;
import cn.itzd.service.UploadService;
import cn.itzd.utils.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Author: dada
 * @Version: 1.0
 * @Description:
 */
@RestController
@Slf4j
public class UploadController {
    @Autowired
    private UploadService uploadFile;

    @PostMapping("upload")
    public R<SysFileVo> upload(MultipartFile file) {
        try {
            // 上传并返回访问地址
            Map<String, String> fileData = uploadFile.uploadFile(file);
            SysFileVo sysFile = new SysFileVo();
            sysFile.setFileName(fileData.get("fileName"));
            sysFile.setSysFileName(fileData.get("newFileName"));
            sysFile.setUrl(fileData.get("pathFileName"));
            sysFile.setUrlIp(fileData.get("pathIpFileName"));
            return R.ok(sysFile);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.failed(e.getMessage());
        }
    }
}
